package com.sywb.chuangyebao.contract;

import android.app.Activity;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.SuperInfo;
import com.sywb.chuangyebao.bean.VideoData;
import com.sywb.chuangyebao.utils.DateTimeUtils;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.view.VideoDetailActivity;

import org.bining.footstone.adapter.BaseRecyclerMultiItemAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.utils.TimeUtils;

import java.util.List;

public interface UserVideoContract {

    interface View extends BaseRefreshContract.View {
        int getUId();
    }

    class Presenter extends BaseRefreshContract.Presenter<View> {

        private SearchAdapter recyclerAdapter;

        private int uid;
        private int pageNumber = 1;

        @Override
        public void onStart() {
            super.onStart();
            uid = mView.getUId();

            recyclerAdapter = new SearchAdapter(mActivity);
            setRecyclerAdapter(recyclerAdapter);
            pullToRefreshView.setRefreshEnable(false);
            recyclerAdapter.setPageSize(1);

            initData();
        }

        @Override
        public void initData() {
            onStartAsync();
            pageNumber = 1;
            getVideoList();
        }

        @Override
        public void onStartRefresh() {
            pageNumber = 1;
            getVideoList();
        }

        @Override
        public void onStartLoadMore() {
            pageNumber++;
            getVideoList();
        }

        private void getVideoList() {
            HttpUtils.getContentList(Constants.TOPIC_TYPE_VIDEO, uid < 0 ? null : String.valueOf(uid), null, pageNumber, null, "1", null, new HttpCallback<List<SuperInfo>>() {

                @Override
                public void onSuccess(List<SuperInfo> superInfos) {
                    if (superInfos != null) {
                        for (SuperInfo superInfo : superInfos) {
                            superInfo.setItemType(superInfo.item_style);
                        }
                    }
                    if (pageNumber == 1) {
                        onEndRefresh();
//                        if (superInfos != null && superInfos.size() > 0) {
//                            recyclerAdapter.setFooterView(R.layout.layout_footer);
//                        } else {
//                            recyclerAdapter.setFooterView(null);
//                        }
                        recyclerAdapter.setFooterView(null);
                        recyclerAdapter.clearDatas();
                    } else {
                        if (superInfos != null && superInfos.size() > 0) {
                            recyclerAdapter.setFooterView(R.layout.layout_footer);
                        } else {
                            recyclerAdapter.setFooterView(null);
                        }
                    }
                    recyclerAdapter.notifyDataChangedAfterLoadMore(superInfos);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    if (pageNumber == 1) {
                        onEndRefresh();
                    } else {
                        onEndLoadMore();
                    }
                    showMessage(strMsg);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                    isNoData();
                }
            });
        }

        public void onRecyclerItemClick(int i) {
            SuperInfo superInfo = recyclerAdapter.getItem(i);
            VideoData videoInfo = JSON.parseObject(superInfo.content, VideoData.class);
            mView.advance(VideoDetailActivity.class, videoInfo.item_id);
        }
    }

    class SearchAdapter extends BaseRecyclerMultiItemAdapter<SuperInfo> {

        private Activity mActivity;

        public SearchAdapter(Activity activity) {
            super(activity, null);
            this.mActivity = activity;
            addItemType(Constants.NEWS_TEXT, R.layout.item_news_text);//文章纯文字布局
            addItemType(Constants.NEWS_RIGHT_PIC_VIDEO, R.layout.item_news_right_pic);//文章右侧小图布局
            addItemType(Constants.NEWS_CENTER_SINGLE_PIC, R.layout.item_news_center_pic);//文章居中大图布局
            addItemType(Constants.NEWS_THREE_PICS, R.layout.item_news_three_pics);//文章三张图片布局
            addItemType(Constants.QANDA_TEXT, R.layout.item_search_qanda);//问答布局
            addItemType(Constants.QANDA_PIC_ONE, R.layout.item_search_qanda);//问答布局
            addItemType(Constants.QANDA_PIC_THREE, R.layout.item_search_qanda);//问答布局
            addItemType(Constants.QANDA_MEDIA, R.layout.item_search_qanda);//问答布局
            addItemType(Constants.VIDEO_LIST, R.layout.item_video_right_pic);//视频布局
            addItemType(Constants.USER_LIST, R.layout.item_search_user);//用户布局
        }

        @Override
        protected int getDefItemViewType(int position) {
            if (position >= getDataCount()) {
                return 0x00000333;
            }
            return super.getDefItemViewType(position);
        }

        @Override
        protected void setMultiData(ViewHolderHelper viewHolderHelper, int i, SuperInfo superInfo) {
            if (superInfo.item_style == Constants.VIDEO_LIST) {
                VideoData videoInfo = JSON.parseObject(superInfo.content, VideoData.class);

                ImageView ivImg = viewHolderHelper.getView(R.id.iv_img);
                GlideUtils.getNormalIamgeToUrl(mActivity, videoInfo.logo, ivImg);

                viewHolderHelper.setText(R.id.tv_title, videoInfo.title);
                viewHolderHelper.setText(R.id.tv_duration, DateTimeUtils.secToTime(videoInfo.length));
                viewHolderHelper.setVisibility(R.id.tv_tag, false);
                viewHolderHelper.setVisibility(R.id.tv_author, false);
                viewHolderHelper.setText(R.id.tv_comment_num, videoInfo.count.getPlayTimesTotal());
                viewHolderHelper.setText(R.id.tv_time, TimeUtils.transformDate10(videoInfo.publish_time));
            }
        }
    }
}

